#pragma once

#include <string>

static std::string dump_mem_to_str(const void *data, int dlen, int slen, int mask)
{
	enum{eAddr = 1, eHex = 2, eChar = 4, eAll=7};
	std::string str;
	char tmp[16]={0};
    uint8_t* p = (uint8_t*) data;
    for (int i = 0; i < dlen; i+=slen, p += slen){

		if(mask&eAddr){
			snprintf(tmp, sizeof(tmp), "%.6x  ", i+slen);
			str += tmp;
		}

		if(mask&eHex){
			for (int j = 0; j < slen; j++) {
				snprintf(tmp, sizeof(tmp), "%.2x ", *(p + j));
				str += tmp;
			}
			str += " ";
		}

		if(mask&eChar){
            for (int k = 0; k < slen; k++){
                unsigned char ch = *(p + k);
                if (!isprint(ch)) 
                    ch = '.';
                snprintf(tmp, sizeof(tmp), "%c", ch);
                str += tmp;
            }
		}
		if(mask&eAll){
		    str += "\n";
		}        
    }
	return str;
}
